<template>
  <div v-loading="loading">
    <el-row>
      <el-col
        style="text-align:left"
        :span="1"
      >
        <el-button
          type="success"
          @click="addBtnClick"
        >添加</el-button>
      </el-col>
      <el-col :span="7">
        <el-input v-model="tableDatas.filters.depname">
          <template #prepend>部门名称</template>
        </el-input>
      </el-col>
    </el-row>

    <el-dialog
      v-model="dialogDatas.dialogFormVisible"
      :title="dialogDatas.isNew ? '添加':'修改'"
      top="5vh"
    >
      <el-scrollbar max-height="70vh">
        <el-form :model="dialogDatas.currentData">
          <el-form-item
            v-for="item in dialogDatas.formItems"
            :label="item.label"
          >
            <el-input
              v-if="!item.isDate"
              v-model="dialogDatas.currentData[item.value]"
              :disabled="item.disableFunction ? item.disableFunction():false"
            />
            <el-date-picker
              v-else
              v-model="dialogDatas.currentData[item.value]"
              :type="item.dateOption ? item.dateOption.type : 'date'"
              :format="item.dateOption ? item.dateOption.format : 'YYYY/MM/DD'"
              :value-format="item.dateOption ? item.dateOption.valueFormat : 'YYYY/MM/DD'"
              :date-format="item.dateOption ? item.dateOption.dateFormat : ''"
              :time-format="item.dateOption ? item.dateOption.timeFormat : ''"
            />
          </el-form-item>
        </el-form>
      </el-scrollbar>

      <template #footer>
        <span>
          <el-button @click="dialogDatas.dialogFormVisible = false">取消</el-button>
          <el-button
            type="primary"
            @click="dialogDatas.confirmBtnClick"
          >
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>

    <el-table
      class="marginTop10"
      :data="tableDatas.tableDatasAfterPage"
      stripe
      border
      empty-text="无数据"
    >
      <el-table-column
        v-for="column in tableDatas.tableColumns"
        :prop="column.prop"
        :label="column.label"
        :align="column.align"
        :width="column.width"
      >
        <template
          #default="scope"
          v-if="column.prop =='operate'"
        >
          <div>
            <el-button
              type="success"
              @click="tableDatas.editBtnClick(scope.row)"
            >修改</el-button>
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              title="确定删除该条记录?"
              @confirm="tableDatas.deleteBtnClick(scope.row)"
            >
              <template #reference>
                <el-button type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="marginTop10"
      @size-change="tableDatas.filters.handleSizeChange"
      @current-change="tableDatas.filters.handleCurrentChange"
      :page-sizes="[10, 15, 20, 100]"
      :page-size="tableDatas.filters.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableDatas.tableDatasAfterFilter.length"
    >
    </el-pagination>

  </div>
</template>
<script>
import { computed, reactive, ref } from 'vue';
import { onMounted, watch } from '@vue/runtime-core';
import myAxios from '@/myAxios/myAxios';
export default {
  name: 'MaintainSpView',
  setup () {
    let loading = ref(false);
    let addBtnClick = function () {
      showFormDialog();
    };

    let showFormDialog = function (row) {
      if (row) {
        dialogDatas.isNew = false;
        dialogDatas.currentData = { ...row };
      }
      else {
        dialogDatas.isNew = true;
        dialogDatas.currentData = {};
      }
      dialogDatas.dialogFormVisible = true;
    };

    let dialogDatas = reactive({
      dialogFormVisible: false,
      currentData: {},
      isNew: false,
      formItems: [
        {
          "value": "DPCODE",
          "label": "部门全称编码",
          "isDate": false,
          "disableFunction": function () {
            return !dialogDatas.isNew;
          }
        },
        {
          "value": "LONGTITLE",
          "label": "部门全称",
          "isDate": false
        },
        {
          "value": "SP_WORKID",
          "label": "审批人工号",
          "isDate": false
        },
        {
          "value": "SP_NAME",
          "label": "审批人姓名",
          "isDate": false
        },
        // {
        //   "value": "DATA1",
        //   "label": "备用字段",
        //   "isDate": false,
        // },
      ],
      confirmBtnClick: function () {
        loading.value = true;

        let params = `${dialogDatas.currentData.DPCODE}\t${dialogDatas.currentData.LONGTITLE}` +
          `\t${dialogDatas.currentData.SP_WORKID}\t${dialogDatas.currentData.SP_NAME}\t${''}`;

        myAxios.post('陈帅廷添加或修改智联补卡系统审批人', params).then(res => {
          if (res.includes('成功')) {
            loading.value = false;
            dialogDatas.dialogFormVisible = false;
            ElMessage({
              type: 'success',
              message: '提交成功!' + res
            });
            getTableDatas();
          }
          else {
            loading.value = false;
            ElMessageBox({
              title: '警告',
              type: 'warning',
              message: '更新失败res!' + res
            });
          }
        }).catch((err) => {
          loading.value = false;
          ElMessageBox({
            title: '警告',
            type: 'warning',
            message: '更新失败err!' + err
          });
        });

      },
      cancelBtnClick: function () {

      }
    });

    let getTableDatas = function () {
      loading.value = true;

      let params = ``;
      myAxios.get('陈帅廷查询智联补卡系统审批人', params).then(res => {
        if (res instanceof Array) {
          loading.value = false;
          tableDatas.oritableData = [...res];
          ElMessage({
            type: 'success',
            message: '最新数据获取成功!'
          });
        }
        else {
          loading.value = false;
          ElMessageBox({
            title: '警告',
            type: 'warning',
            message: '获取数据失败res!' + res
          });
        }
      }).catch((err) => {
        loading.value = false;
        ElMessageBox({
          title: '警告',
          type: 'warning',
          message: '获取数据失败err!' + err
        });
      });
    };

    onMounted(() => {
      getTableDatas();
    });

    let tableDatas = reactive({
      //表格筛选展示相关代码
      filters: {
        depname: '',

        //表格翻页相关代码
        currentPage: 1,
        pagesize: 10,
        handleSizeChange (val) {
          tableDatas.filters.pagesize = val;
        },
        handleCurrentChange (val) {
          tableDatas.filters.currentPage = val;
        },
      },
      tableDatasAfterFilter: computed(() => {
        let fData = tableDatas.oritableData;

        if (tableDatas.filters.depname) {
          fData = fData.filter(item => item.LONGTITLE.includes(tableDatas.filters.depname));
        }

        return fData;
      }),
      tableDatasAfterPage: computed(() => {
        let fData = tableDatas.tableDatasAfterFilter;
        fData = fData.slice((tableDatas.filters.currentPage - 1) * tableDatas.filters.pagesize, tableDatas.filters.currentPage * tableDatas.filters.pagesize);
        return fData;
      }),

      tableColumns: [
        {
          "prop": "DPCODE",
          "label": "部门全称编码",
          "align": "center",
        },
        {
          "prop": "LONGTITLE",
          "label": "部门全称",
          "align": "center",
        },
        {
          "prop": "SP_WORKID",
          "label": "审批人工号",
          "align": "center",
        },
        {
          "prop": "SP_NAME",
          "label": "审批人姓名",
          "align": "center",
        },
        // {
        //   "prop": "DATA1",
        //   "label": "备用字段",
        //   "align": "center",
        // },
        {
          "prop": "operate",
          "label": "操作",
          "align": "center",
          "width": 170,
        }
      ],
      oritableData: [
      ],
      editBtnClick: function (row) {
        showFormDialog(row);
      },
      deleteBtnClick: function (row) {
        loading.value = true;

        let params = `${row.DPCODE}`;
        myAxios.post('陈帅廷删除智联补卡系统审批人', params).then(res => {
          if (res.includes('成功')) {
            loading.value = false;
            dialogDatas.dialogFormVisible = false;
            ElMessage({
              type: 'success',
              message: '删除成功!' + res
            });
            tableDatas.oritableData.splice(tableDatas.oritableData.indexOf(row), 1);
          }
          else {
            loading.value = false;
            ElMessageBox({
              title: '警告',
              type: 'warning',
              message: '删除失败res!' + res
            });
          }
        }).catch((err) => {
          loading.value = false;
          ElMessageBox({
            title: '警告',
            type: 'warning',
            message: '删除失败err!' + err
          });
        });

      },
    });

    return {
      loading,
      addBtnClick,
      dialogDatas,
      tableDatas
    };
  }
}
</script>
<style lang="css" scope>
.marginTop10 {
  margin-top: 10px;
}
</style>

